package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface UserInfoMapper {
    @ResultMap("resultMap")   //复用结果映射关系
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll();

    //在application.yml里面配置驼峰自动转换
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll2();

    @Select("select * from userinfo where delete_flag=#{deleteFlag}")
    List<UserInfo> getUserInfoByDeleteFlag(Integer deleteFlag);


    @Select("select * from userinfo where username = #{userName}")
    UserInfo getUserByName(String userName);

    @Select("select * from userinfo where username = '${userName}'")
    UserInfo getUserByName2(String userName);

    @Select("select * from userinfo where delete_flag=#{deleteFlag} and gender=#{gender}")
    List<UserInfo> getUserInfo2(Integer deleteFlag,Integer gender);

    @Select("select * from userinfo where id=#{id}")
    List<UserInfo> getUserInfo3(Integer id);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo (username, password, age, gender) " +
            "values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer insert(@Param("userInfo")UserInfo userInfo);

    @Delete("delete from userinfo where id=#{id}")
    Integer delete(Integer id);

    @Update("update userinfo set age=#{age},gender=#{gender},delete_flag=#{deleteFlag} where id=#{id}")
    Integer update(UserInfo userInfo);

    /**
     * 取别名
     * @return
     */
    @Select("select id,username,password,age,phone,delete_flag as deleteFlag, " +
            "create_time as createTime,update_time as updateTime from userinfo")
    List<UserInfo> selectUserInfos();

    /**
     * 指定结果映射关系
     * @return
     */
    @Results(id="resultMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "dupdate_time",property = "pdateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> selectUserInfos2();

    @Select("select * from userinfo where username='${username}' and password='${password}'")
    UserInfo queryUserByNameAndPassword(String username, String password);

    @Select("select * from userinfo order by id asc")
    List<UserInfo> queryUserListByOrder(String order);

    @Select("select * from userinfo where username like CONCAT('%',#{username},'%')")
    List<UserInfo> queryUserListByLike(String username);


    @Insert("<script>" +
            "insert into userinfo " +
            "<trim prefix='(' suffix=')' suffixOverrides=','> " +
            "    <if test='username!=null'>username,</if> " +
            "    <if test='password !=null'>password,</if> " +
            "    <if test='age !=null'>age, </if> " +
            "    <if test='gender != null'> gender</if> " +
            "</trim> " +
            " values " +
            "<trim prefix='(' suffix=')' suffixOverrides=','> " +
            "    <if test='username!=null'> #{username},</if> " +
            "    <if test='password !=null'>#{password},</if> " +
            "    <if test='age !=null'>#{age},</if> " +
            "    <if test='gender !=null'> #{gender}</if> " +
            "</trim>" +
            "</script>")
    Integer insertByCondtion(UserInfo userInfo);


}
